/****************************************************************************
*
* Copyright(c) 2023  C*Core -   All Rights Reserved
*
* THIS SOFTWARE IS DISTRIBUTED "AS IS, " AND ALL WARRANTIES ARE DISCLAIMED,
* INCLUDING MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* PROJECT     : CCFC2011BC
* DESCRIPTION : CCFC2011BC adc low level drivers common code
* HISTORY     : Initial version.
* @file     adc_lld.c
* @version  1.1
* @date     2023 - 02 - 20
* @brief    Initial version.
*
*****************************************************************************/
#include "adc_lld.h"
#include "edma_lld.h"
#include "lldconf.h"
#include "intc_lld.h"

/*****************************************************************************************
 * 2 ADC modules, (ADC_0 with 10 - bit resolution and ADC_1 with 12 - bit resolution)
 *****************************************************************************************
 * Precision channels:
 *   ADC0_P[0]~ADC0_P[15](CH0~CH15),  ADC1_P[0]~ADC1_P[15](CH0~CH15)
 * Standard channels:
 *   ADC0_S[0]~ADC0_S[27](CH32~CH59), ADC1_S[0]~ADC1_S[7](CH32~CH39)
 * External multiplexed channels:
 *   ADC0_X[0](CH64~CH71), ADC0_X[1](CH72~CH79), ADC0_X[2](CH80~CH87), ADC0_X[3](CH88~CH95)
 * Chip packaging:
 *   60L7_LQFP176 /60L5_LQFP144 /50L5_LQFP144 /50L3_LQFP100 /50L1_LQFP64
 *****************************************************************************************
 * Precision channels:                    * Chip packaging:
 * PB4 : ADC0_CH0,  ADC1_CH0              * All have
 * PB5 : ADC0_CH1,  ADC1_CH1              * All have
 * PB6 : ADC0_CH2,  ADC1_CH2              * All have
 * PB7 : ADC0_CH3,  ADC1_CH3              * All have
 * PD0 : ADC0_CH4,  ADC1_CH4              * No pin: 50L1_LQFP64
 * PD1 : ADC0_CH5,  ADC1_CH5              * No pin: 50L1_LQFP64
 * PD2 : ADC0_CH6,  ADC1_CH6              * No pin: 50L1_LQFP64
 * PD3 : ADC0_CH7,  ADC1_CH7              * No pin: 50L1_LQFP64
 * PD4 : ADC0_CH8,  ADC1_CH8              * No pin: 50L1_LQFP64
 * PD5 : ADC0_CH9,  ADC1_CH9              * No pin: 50L1_LQFP64
 * PD6 : ADC0_CH10, ADC1_CH10             * No pin: 50L1_LQFP64
 * PD7 : ADC0_CH11, ADC1_CH11             * No pin: 50L1_LQFP64
 * PD8 : ADC0_CH12, ADC1_CH12             * No pin: 50L1_LQFP64
 * PD9 : ADC0_CH13, ADC1_CH13             * No pin: 50L1_LQFP64
 * PD10: ADC0_CH14, ADC1_CH14             * No pin: 50L1_LQFP64
 * PD11: ADC0_CH15, ADC1_CH15             * No pin: 50L1_LQFP64
 ******************************************
 * Standard channels:                     *
 * PB8 : ADC0_CH32, ADC1_CH36             * All have
 * PB9 : ADC0_CH33, ADC1_CH37             * All have
 * PB10: ADC0_CH34, ADC1_CH38             * All have
 * PB11: ADC0_CH35, (*******)             * No pin: 60L5_LQFP144
 * PD12: ADC0_CH36, (*******)             * No pin: 60L5_LQFP144/50L1_LQFP64
 * PD13: ADC0_CH37, (*******)             * No pin: 50L1_LQFP64
 * PD14: ADC0_CH38, (*******)             * No pin: 50L1_LQFP64
 * PD15: ADC0_CH39, (*******)             * No pin: 50L1_LQFP64
 * PF0 : ADC0_CH40, (*******)             * No pin: 50L3_LQFP100/50L1_LQFP64
 * PF1 : ADC0_CH41, (*******)             * No pin: 50L3_LQFP100/50L1_LQFP64
 * PF2 : ADC0_CH42, (*******)             * No pin: 50L3_LQFP100/50L1_LQFP64
 * PF3 : ADC0_CH43, (*******)             * No pin: 50L3_LQFP100/50L1_LQFP64
 * PF4 : ADC0_CH44, (*******)             * No pin: 50L3_LQFP100/50L1_LQFP64
 * PF5 : ADC0_CH45, (*******)             * No pin: 50L3_LQFP100/50L1_LQFP64
 * PF6 : ADC0_CH46, (*******)             * No pin: 50L3_LQFP100/50L1_LQFP64
 * PF7 : ADC0_CH47, (*******)             * No pin: 50L3_LQFP100/50L1_LQFP64
 * PI8 : ADC0_CH48, (*******)             * Only 60L7_LQFP176 have
 * PI9 : ADC0_CH49, (*******)             * Only 60L7_LQFP176 have
 * PI10: ADC0_CH50, (*******)             * Only 60L7_LQFP176 have
 * PI11: ADC0_CH51, (*******)             * Only 60L7_LQFP176 have
 * PI12: ADC0_CH52, (*******)             * Only 60L7_LQFP176 have
 * PI13: ADC0_CH53, (*******)             * Only 60L7_LQFP176 have
 * PI14: ADC0_CH54, (*******)             * Only 60L7_LQFP176 have
 * PI15: ADC0_CH55, (*******)             * Only 60L7_LQFP176 have
 * PJ0 : ADC0_CH56, (*******)             * Only 60L7_LQFP176 have
 * PJ1 : ADC0_CH57, (*******)             * Only 60L7_LQFP176 have
 * PJ2 : ADC0_CH58, (*******)             * Only 60L7_LQFP176 have
 * PJ3 : ADC0_CH59, (*******)             * Only 60L7_LQFP176 have
 * PA3 : (*******), ADC1_CH32             * All have
 * PA7 : (*******), ADC1_CH33             * All have
 * PA10: (*******), ADC1_CH34             * All have
 * PA11: (*******), ADC1_CH35             * All have
 * PE12: (*******), ADC1_CH39             * No pin: 50L1_LQFP64
 ******************************************
 * External multiplexed channels:         *
 * PB12: ADC0_X[0](ADC0_CH64 ~ ADC0_CH71) * All have
 * PB13: ADC0_X[1](ADC0_CH72 ~ ADC0_CH79) * All have
 * PB14: ADC0_X[2](ADC0_CH80 ~ ADC0_CH87) * All have
 * PB15: ADC0_X[3](ADC0_CH88 ~ ADC0_CH95) * All have
 *****************************************************************************************
 * */

static const uint8_t ADC0_PIN[60] = {
    ADC0_CH0_PCR,
    ADC0_CH1_PCR,
    ADC0_CH2_PCR,
    ADC0_CH3_PCR,
    ADC0_CH4_PCR,
    ADC0_CH5_PCR,
    ADC0_CH6_PCR,
    ADC0_CH7_PCR,
    ADC0_CH8_PCR,
    ADC0_CH9_PCR,
    ADC0_CH10_PCR,
    ADC0_CH11_PCR,
    ADC0_CH12_PCR,
    ADC0_CH13_PCR,
    ADC0_CH14_PCR,
    ADC0_CH15_PCR,
    0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U,

    ADC0_CH32_PCR,
    ADC0_CH33_PCR,
    ADC0_CH34_PCR,
    ADC0_CH35_PCR,
    ADC0_CH36_PCR,
    ADC0_CH37_PCR,
    ADC0_CH38_PCR,
    ADC0_CH39_PCR,
    ADC0_CH40_PCR,
    ADC0_CH41_PCR,
    ADC0_CH42_PCR,
    ADC0_CH43_PCR,
    ADC0_CH44_PCR,
    ADC0_CH45_PCR,
    ADC0_CH46_PCR,
    ADC0_CH47_PCR,
    ADC0_CH48_PCR,
    ADC0_CH49_PCR,
    ADC0_CH50_PCR,
    ADC0_CH51_PCR,
    ADC0_CH52_PCR,
    ADC0_CH53_PCR,
    ADC0_CH54_PCR,
    ADC0_CH55_PCR,
    ADC0_CH56_PCR,
    ADC0_CH57_PCR,
    ADC0_CH58_PCR,
    ADC0_CH59_PCR
};

static const uint8_t ADC1_PIN[40] = {
    ADC1_CH0_PCR,
    ADC1_CH1_PCR,
    ADC1_CH2_PCR,
    ADC1_CH3_PCR,
    ADC1_CH4_PCR,
    ADC1_CH5_PCR,
    ADC1_CH6_PCR,
    ADC1_CH7_PCR,
    ADC1_CH8_PCR,
    ADC1_CH9_PCR,
    ADC1_CH10_PCR,
    ADC1_CH11_PCR,
    ADC1_CH12_PCR,
    ADC1_CH13_PCR,
    ADC1_CH14_PCR,
    ADC1_CH15_PCR,
    0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U,

    ADC1_CH32_PCR,
    ADC1_CH33_PCR,
    ADC1_CH34_PCR,
    ADC1_CH35_PCR,
    ADC1_CH36_PCR,
    ADC1_CH37_PCR,
    ADC1_CH38_PCR,
    ADC1_CH39_PCR
};


/*******************************************************************************
 * @brief      ADC config
 * @param[in]  ADC_Driver    adcd
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void ADC_ConfigInit(const ADC_Driver *adcd) /* PRQA S 1505 */
{
    if (adcd->index == ADC0_SELECT)
    {
        adcd->ADC0.adc0_tag->MCR.B.ACKO = adcd->ADC0.config->ACKO;
        adcd->ADC0.adc0_tag->MCR.B.ADCLKSEL = adcd->ADC0.config->ADCLKSEL;
        adcd->ADC0.adc0_tag->MCR.B.MODE = adcd->ADC0.config->MODE;
        adcd->ADC0.adc0_tag->MCR.B.WLSIDE = adcd->ADC0.config->WLSIDE;
        adcd->ADC0.adc0_tag->MCR.B.OWREN = adcd->ADC0.config->OWREN;
        adcd->ADC0.adc0_tag->MCR.B.PWDN = adcd->ADC0.config->PWDN;
    }

    if (adcd->index == ADC1_SELECT)
    {
        adcd->ADC1.adc1_tag->MCR.B.ACKO = adcd->ADC1.config->ACKO;
        adcd->ADC1.adc1_tag->MCR.B.ADCLKSEL = adcd->ADC1.config->ADCLKSEL;
        adcd->ADC1.adc1_tag->MCR.B.MODE = adcd->ADC1.config->MODE;
        adcd->ADC1.adc1_tag->MCR.B.WLSIDE = adcd->ADC1.config->WLSIDE;
        adcd->ADC1.adc1_tag->MCR.B.OWREN = adcd->ADC1.config->OWREN;
        adcd->ADC1.adc1_tag->MCR.B.PWDN = adcd->ADC1.config->PWDN;
    }
}

/*******************************************************************************
 * @brief      init ADC
 * @param[in]  adcd      pointer to the @p ADC_Driver object
 *             config     loadvalue
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void ADC_LLD_Cfg_Init(ADC_Driver *adcd, const ADC_Config *config)
{
    if (adcd->index == ADC0_SELECT)
    {
        adcd->ADC0.adc0_tag = &ADC_0;
        adcd->ADC0.config = config;
        ADC_ConfigInit(adcd);
    }

    if (adcd->index == ADC1_SELECT)
    {
        adcd->ADC1.adc1_tag = &ADC_1;
        adcd->ADC1.config = config;
        ADC_ConfigInit(adcd);
    }
}

/*******************************************************************************
 * @brief      select ADC channel in normal mode
 * @param[in]  adcd      pointer to the @p ADC_Driver object
 *             channel     the channel selected
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void ADC_LLD_ChannelConfig(const ADC_Driver *adcd, uint32_t channel)
{
    if (adcd->index == ADC0_SELECT)
    {
        if (channel < 16U)
        {
            //SIUL_Init_ADC(ADC0_PIN[channel]);
            adcd->ADC0.adc0_tag->NCMR0.R |= (1UL << channel);
        }
        else if (channel < 32U)
        {
            /* Without these channels: Ch16 ~ Ch31 */
        }
        else if (channel < 60U)
        {
            //SIUL_Init_ADC(ADC0_PIN[channel]);
            adcd->ADC0.adc0_tag->NCMR1.R |= (1UL << (channel - 32U));
            /* Without these channels: Ch60 ~ Ch63 */
        }
        else if ((channel >= 64U) && (channel < 95U))
        {
            if (channel >= 88u)
            {
                //SIUL_Init_ADC(ADC0_EX0_PCR);
                //SIUL_Init_ADC(ADC0_EX1_PCR);
                //SIUL_Init_ADC(ADC0_EX2_PCR);
                //SIUL_Init_ADC(ADC0_EX3_PCR);
            }
            else if (channel >= 80U)
            {
                //SIUL_Init_ADC(ADC0_EX0_PCR);
                //SIUL_Init_ADC(ADC0_EX1_PCR);
                //SIUL_Init_ADC(ADC0_EX2_PCR);
            }
            else if (channel >= 72U)
            {
                //SIUL_Init_ADC(ADC0_EX0_PCR);
                //SIUL_Init_ADC(ADC0_EX1_PCR);
            }
            else
            {
                /* < 72 */
                //SIUL_Init_ADC(ADC0_EX0_PCR);
            }
            adcd->ADC0.adc0_tag->NCMR2.R |= (1UL << (channel - 64U));
        }
        else
        {
            /* Nothing. */
        }
    }

    if (adcd->index == ADC1_SELECT)
    {
        if (channel < 16U)
        {
            //SIUL_Init_ADC(ADC1_PIN[channel]);
            adcd->ADC1.adc1_tag->NCMR0.R |= (1UL << channel);
        }
        else if (channel < 32U)
        {
            /* Without these channels: Ch16 ~ Ch31 */
        }
        else if (channel < 40U)
        {
            //SIUL_Init_ADC(ADC1_PIN[channel]);
            adcd->ADC1.adc1_tag->NCMR1.R |= (1UL << (channel - 32U));
            /* Without these channels: Ch40 ~ Ch63 */
        }
        else
        {
            /* Nothing. */
        }
    }
}

/*******************************************************************************
 * @brief      select ADC channel in injected mode
 * @param[in]  adcd      pointer to the @p ADC_Driver object
 *             channel     the channel selected
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void ADC_LLD_InjectedChannelConfig(const ADC_Driver *adcd, uint32_t channel)
{
    if (adcd->index == ADC0_SELECT)
    {
        if (channel < 16U)
        {
            SIUL_Init_ADC(ADC0_PIN[channel]);
            adcd->ADC0.adc0_tag->JCMR0.R |= (1UL << channel);
        }
        else if (channel < 32U)
        {
            /* Without these channels: Ch16 ~ Ch31 */
        }
        else if (channel < 60U)
        {
            SIUL_Init_ADC(ADC0_PIN[channel]);
            adcd->ADC0.adc0_tag->JCMR1.R |= (1UL << (channel - 32U));
            /* Without these channels: Ch60 ~ Ch63 */
        }
        else if ((channel >= 64U) && (channel < 95U))
        {
            if (channel >= 88u)
            {
                SIUL_Init_ADC(ADC0_EX0_PCR);
                SIUL_Init_ADC(ADC0_EX1_PCR);
                SIUL_Init_ADC(ADC0_EX2_PCR);
                SIUL_Init_ADC(ADC0_EX3_PCR);
            }
            else if (channel >= 80U)
            {
                SIUL_Init_ADC(ADC0_EX0_PCR);
                SIUL_Init_ADC(ADC0_EX1_PCR);
                SIUL_Init_ADC(ADC0_EX2_PCR);
            }
            else if (channel >= 72U)
            {
                SIUL_Init_ADC(ADC0_EX0_PCR);
                SIUL_Init_ADC(ADC0_EX1_PCR);
            }
            else
            {
                /* < 72 */
                SIUL_Init_ADC(ADC0_EX0_PCR);
            }
            adcd->ADC0.adc0_tag->JCMR2.R |= (1UL << (channel - 64U));
        }
        else
        {
            /* Nothing. */
        }
    }

    if (adcd->index == ADC1_SELECT)
    {
        if (channel < 16U)
        {
            SIUL_Init_ADC(ADC1_PIN[channel]);
            adcd->ADC1.adc1_tag->JCMR0.R |= (1UL << channel);
        }
        else if (channel < 32U)
        {
            /* Without these channels: Ch16 ~ Ch31 */
        }
        else if (channel < 40U)
        {
            SIUL_Init_ADC(ADC1_PIN[channel]);
            adcd->ADC1.adc1_tag->JCMR1.R |= (1UL << (channel - 32U));
            /* Without these channels: Ch40 ~ Ch63 */
        }
        else
        {
            /* Nothing. */
        }
    }
}

/*******************************************************************************
 * @brief      ADC interrupt enable
 * @param[in]  adcd      pointer to the @p ADC_Driver object
 *             mode      interrupt mode
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void ADC_LLD_IntcConfig(const ADC_Driver *adcd, uint32_t mode)
{
    if (adcd->index == ADC0_SELECT)
    {
        adcd->ADC0.adc0_tag->IMR.R = mode;
    }

    if (adcd->index == ADC1_SELECT)
    {
        adcd->ADC1.adc1_tag->IMR.R = mode;
    }
}

/*******************************************************************************
 * @brief      ADC interrupt enable
 * @param[in]  adcd      pointer to the @p ADC_Driver object
 *             channel
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void ADC_LLD_IntcEnable(const ADC_Driver *adcd, uint32_t channel)
{
    if (adcd->index == ADC0_SELECT)
    {
        if (channel < 16U)
        {
            adcd->ADC0.adc0_tag->CIMR0.R |= (1UL << channel);
        }
        else if (channel < 32U)
        {
            /* Without these channels: Ch16 ~ Ch31 */
        }
        else if (channel < 60U)
        {
            adcd->ADC0.adc0_tag->CIMR1.R |= (1UL << (channel - 32U));
            /* Without these channels: Ch60 ~ Ch63 */
        }
        else if ((channel >= 64U) && (channel < 95U))
        {
            adcd->ADC0.adc0_tag->CIMR2.R |= (1UL << (channel - 64U));
        }
        else
        {
            /* Nothing. */
        }
    }

    if (adcd->index == ADC1_SELECT)
    {
        if (channel < 16U)
        {
            adcd->ADC1.adc1_tag->CIMR0.R |= (1UL << channel);
        }
        else if (channel < 32U)
        {
            /* Without these channels: Ch16 ~ Ch31 */
        }
        else if (channel < 40U)
        {
            adcd->ADC1.adc1_tag->CIMR1.R |= (1UL << (channel - 32U));
            /* Without these channels: Ch40 ~ Ch63 */
        }
        else
        {
            /* Nothing. */
        }
    }
}

/*******************************************************************************
 * @brief      get ADC value
 * @param[in]  adcd      pointer to the @p ADC_Driver object
 *             channel
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
uint16_t ADC_LLD_ChanValue(const ADC_Driver *adcd, uint32_t channel)
{
    uint16_t result = 0U;

    if (adcd->index == ADC0_SELECT)
    {
        result = adcd->ADC0.adc0_tag->CDR[channel].B.CDATA;
    }
    if (adcd->index == ADC1_SELECT)
    {
        result = adcd->ADC1.adc1_tag->CDR[channel].B.CDATA;
    }

    return result;
}

/*******************************************************************************
 * @brief      ADC Conversion Timing Set
 * @param[in]  adcd      pointer to the @p ADC_Driver object
 *             ctrNum  ADC Conversion Timing Registers number
 *             inpcmp  Configuration bits for comparison phase duration
 *             inpsamp Configuration bits for sampling phase duration
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void ADC_LLD_ConversionTiming(const ADC_Driver *adcd, uint8_t ctrNum, uint32_t inpcmp, uint32_t inpsamp)
{
    if (adcd->index == ADC0_SELECT)
    {
        if (ADC_CTR0_CH0_CH15 == (ctrNum & ADC_CTR0_CH0_CH15))
        {
            adcd->ADC0.adc0_tag->CTR0.R = inpcmp|inpsamp;
        }
        if (ADC_CTR1_CH32_CH59 == (ctrNum & ADC_CTR1_CH32_CH59))
        {
            adcd->ADC0.adc0_tag->CTR1.R = inpcmp|inpsamp;
        }
        if (ADC_CTR2_CH64_CH95 == (ctrNum & ADC_CTR2_CH64_CH95))
        {
            adcd->ADC0.adc0_tag->CTR2.R = inpcmp|inpsamp;
        }
    }

    if (adcd->index == ADC1_SELECT)
    {
        if (ADC_CTR0_CH0_CH15 == (ctrNum & ADC_CTR0_CH0_CH15))
        {
            adcd->ADC1.adc1_tag->CTR0.R = inpcmp|inpsamp;
        }
        if (ADC_CTR1_CH32_CH59 == (ctrNum & ADC_CTR1_CH32_CH59))
        {
            adcd->ADC1.adc1_tag->CTR1.R = inpcmp|inpsamp;
        }
    }
}

/*******************************************************************************
 * @brief      set ADC Clock Divide
 * @param[in]  adcd      pointer to the @p ADC_Driver object
 *             div       Divide
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void ADC_LLD_SetClockDivide(const ADC_Driver *adcd, uint8_t clkDiv)
{
    if (adcd->index == ADC0_SELECT)
    {
        adcd->ADC0.adc0_tag->MCR.B.ADCLKSEL = clkDiv;
    }
    if (adcd->index == ADC1_SELECT)
    {
        adcd->ADC1.adc1_tag->MCR.B.ADCLKSEL = clkDiv;
    }
}

/*******************************************************************************
 * @brief      start ADC normal
 * @param[in]  adcd      pointer to the @p ADC_Driver object
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void ADC_LLD_Start(const ADC_Driver *adcd)
{
    if (adcd->index == ADC0_SELECT)
    {
        adcd->ADC0.adc0_tag->MCR.B.PWDN = 0U;
        adcd->ADC0.adc0_tag->MCR.B.NSTART = 1U;
    }

    if (adcd->index == ADC1_SELECT)
    {
        adcd->ADC1.adc1_tag->MCR.B.PWDN = 0U;
        adcd->ADC1.adc1_tag->MCR.B.NSTART = 1U;
    }
}

/*******************************************************************************
 * @brief      stop ADC
 * @param[in]  adcd      pointer to the @p ADC_Driver object
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void ADC_Stop(const ADC_Driver *adcd)
{
    if (adcd->index == ADC0_SELECT)
    {
        adcd->ADC0.adc0_tag->MCR.B.NSTART = 0U;
        adcd->ADC0.adc0_tag->MCR.B.PWDN = 1U;
    }
    if (adcd->index == ADC1_SELECT)
    {
        adcd->ADC1.adc1_tag->MCR.B.NSTART = 0U;
        adcd->ADC1.adc1_tag->MCR.B.PWDN = 1U;
    }
}

/*******************************************************************************
 * @brief      End of Chain Conversion interrupt flag
 * @param[in]  adcd      pointer to the @p ADC_Driver object
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
uint8_t ADC_ECH_IntFlag(const ADC_Driver *adcd)
{
    uint8_t flag = 0U;

    if (adcd->index == ADC0_SELECT)
    {
        flag = adcd->ADC0.adc0_tag->ISR.B.ECH;
    }
    if (adcd->index == ADC1_SELECT)
    {
        flag = adcd->ADC1.adc1_tag->ISR.B.ECH;
    }

    return flag;
}

/*******************************************************************************
 * @brief      End of Injected Chain Conversion interrupt flag
 * @param[in]  adcd      pointer to the @p ADC_Driver object
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
uint8_t ADC_JECH_IntFlag(const ADC_Driver *adcd)
{
    uint8_t flag = 0U;

    if (adcd->index == ADC0_SELECT)
    {
        flag = adcd->ADC0.adc0_tag->ISR.B.JECH;
    }
    if (adcd->index == ADC1_SELECT)
    {
        flag = adcd->ADC1.adc1_tag->ISR.B.JECH;
    }

    return flag;
}

/*******************************************************************************
 * @brief      set trigger mode
 * @param[in]  adcd      pointer to the @p ADC_Driver object
 *             mode      trigger mode
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void ADC_LLD_TriggerMode(const ADC_Driver *adcd, uint8_t mode)
{
    if (adcd->index == ADC0_SELECT)
    {
        adcd->ADC0.adc0_tag->MCR.B.CTUEN = mode;
    }
    if (adcd->index == ADC1_SELECT)
    {
        adcd->ADC1.adc1_tag->MCR.B.CTUEN = mode;
    }
}

/*******************************************************************************
 * @brief      init ADC wdg interrupt
 * @param[in]  adcd      pointer to the @p ADC_Driver object
 *             edge
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void ADC_LLD_WDGInit(const ADC_Driver *adcd)
{
    uint32_t i = 0;
    uint32_t temp = 0;
    uint32_t channel_enable = 0;
    if (adcd->index == ADC0_SELECT)
    {
        for (i = 0U; i < ADC0_THRHLR_MAX_NUM; i++)
        {
            
            if(adcd->WDG_t->wdg_enable[i] == TRUE)
            {
            	adcd->ADC0.adc0_tag->TCR[i].R = (uint32_t)( 1 << 15 | adcd->WDG_t->wdg_select[i]);
            	if(adcd->WDG_t->threshold_h[i] > 1023)
            		adcd->WDG_t->threshold_h[i] = 1023;
            	if(adcd->WDG_t->threshold_l[i] > 1023)
            		adcd->WDG_t->threshold_l[i] = 1023;
            	
            	temp = (uint32_t)adcd->WDG_t->threshold_h[i] << 16U | (uint32_t)adcd->WDG_t->threshold_l[i];
            	
            	adcd->ADC0.adc0_tag->THRHLR[i].R = temp;
            }
            
//            channel_enable += 0x11 << adcd->WDG_t->wdg_enable[i];
        }
        
        adcd->ADC0.adc0_tag->WTIMR.R = 0xFF;
    }

}

/*******************************************************************************
 * @brief      set trigger mode
 * @param[in]  adcd      pointer to the @p ADC_Driver object
 *             edge
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void ADC_LLD_CTUTriggerEnable(const ADC_Driver *adcd , uint8_t edge)
{
    if (adcd->index == ADC0_SELECT)
    {
        adcd->ADC0.adc0_tag->MCR.B.JTRGEN = 1;
        adcd->ADC0.adc0_tag->MCR.B.CTUEN = 1;
        adcd->ADC0.adc0_tag->MCR.B.JEDGE = edge;
    }
    if (adcd->index == ADC1_SELECT)
    {
        adcd->ADC1.adc1_tag->MCR.B.JTRGEN = 1;
        adcd->ADC1.adc1_tag->MCR.B.CTUEN = 1;
        adcd->ADC1.adc1_tag->MCR.B.JEDGE = edge;
    }
}

/*******************************************************************************
 * @brief      start ADC injected
 * @param[in]  adcd      pointer to the @p ADC_Driver object
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void ADC_LLD_InjectedStart(const ADC_Driver *adcd)
{
    if (adcd->index == ADC0_SELECT)
    {
        adcd->ADC0.adc0_tag->MCR.B.JSTART = 1;
    }
    if (adcd->index == ADC1_SELECT)
    {
        adcd->ADC1.adc1_tag->MCR.B.JSTART = 1;
    }
}


/*******************************************************************************
 * @brief      enable CTU trigger
 * @param[in]  adcd      pointer to the @p ADC_Driver object
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
void ADC_LLD_CTU_Enable(const ADC_Driver *adcd)
{
    if (adcd->index == ADC0_SELECT)
    {
        adcd->ADC0.adc0_tag->MCR.B.CTUEN = 1;
    }
    if (adcd->index == ADC1_SELECT)
    {
        adcd->ADC1.adc1_tag->MCR.B.CTUEN = 1;
    }
}

/*******************************************************************************
 * @brief      ADC_0_Receive
 * @param[in]  uint8_t Channel
 * @param[out] None
 * @retval     None
 * @notapi
 *******************************************************************************/
uint16_t ADC_0_Receive(uint8_t Channel)
{
    return ADC_0.CDR[Channel].B.CDATA;
}
